package com.babble.comment.service.impl;

import com.babble.comment.conversion.ReportMapper;
import com.babble.comment.dto.QueryReportDTO;
import com.babble.comment.dto.ReportDTO;
import com.babble.comment.entity.Report;
import com.babble.comment.repository.ReportRepository;
import com.babble.comment.service.IReportService;
import com.babble.comment.vo.ReportVo;
import com.babble.user.dto.UserOrToUserDTO;
import com.babble.user.feign.IUserClient;
import lombok.RequiredArgsConstructor;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 举报评论服务实现
 *
 * @author wm
 */
@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements IReportService {
	private final ReportRepository reportRepository;
	private final MongoTemplate mongoTemplate;
	private final ReportMapper reportMapper;
	private final IUserClient userClient;

	@Override
	public Report create(ReportDTO reportDTO) {
		BladeUser user = SecureUtil.getUser();
		Report report = reportMapper.reportDtoToReport(reportDTO);
		report.setInformantId(String.valueOf(user.getUserId()));
//		report.setInformantId(RandomUtil.randomNumbers(19));
		report.setProgress(0);
		return reportRepository.save(report);
	}

	@Override
	public Page<ReportVo> list(QueryReportDTO queryReport) {
		Criteria criteria = new Criteria();
		//查询条件
		if (Func.isNotEmpty(queryReport.getContent())) {
			Pattern pattern = Pattern.compile("^.*" + queryReport.getContent() + ".*$", Pattern.CASE_INSENSITIVE);
			criteria.and("content").regex(pattern);
		}
		if (Func.isNotEmpty(queryReport.getSource())) {
			criteria.and("source").is(queryReport.getSource());
		}
		if (Func.isNotEmpty(queryReport.getDefendantId())) {
			criteria.and("defendant_id").is(queryReport.getDefendantId());
		}
		Sort.Direction sort = Sort.Direction.DESC;
		if ("asc".equalsIgnoreCase(queryReport.getAscs())) {
			sort = Sort.Direction.ASC;
		}
		Query query = new Query(criteria);
		//分页条件
		PageRequest pageable = PageRequest.of(queryReport.getCurrent() - 1, queryReport.getSize(), sort, "create_time");
		//获取查询数据总数
		long count = mongoTemplate.count(query, Report.class);
		List<Report> reports = mongoTemplate.find(query.with(pageable), Report.class);
		List<ReportVo> reportVos = reports.stream().parallel().map(report -> {
			ReportVo reportVo = reportMapper.reportToReportVo(report);
			R<UserOrToUserDTO> userOrToUserDTOR = userClient.userInfoOrToUserinfo(report.getInformantId(), report.getDefendantId());
			if (userOrToUserDTOR.isSuccess()) {
				UserOrToUserDTO userOrToUserDTO = userOrToUserDTOR.getData();
				reportVo.setAvatar(userOrToUserDTO.getAvatar());
				reportVo.setNickName(userOrToUserDTO.getNickName());
				reportVo.setToAvatar(userOrToUserDTO.getToAvatar());
				reportVo.setToNickName(userOrToUserDTO.getToNickName());
			}
			return reportVo;
		}).collect(Collectors.toList());
		//封装分页
		return PageableExecutionUtils.getPage(reportVos, pageable, () -> count);
	}

	@Override
	public Report updateProgress(String id, Integer progress) {
		Report report = reportRepository.findById(id).orElse(null);
		if (report == null) {
			return null;
		}
		report.setProgress(progress);
		return reportRepository.save(report);
	}

	@Override
	public void delete(String id) {
		reportRepository.deleteById(id);
	}
}
